草庐IT

php - MongoDB php $in 和 $regex

全部标签

regex - Golang 正则表达式匹配字符串直到给定的字符序列

我有一个字符串,可以有一个-name后跟值(可以有空格),也可以有-descr后跟一个值(-descr后跟值可能不存在):示例字符串:runcmd-nameabcdxyz-descrabcdef或runcmd-nameabcdxyz使用Go语言,我该如何编写正则表达式,它会返回-descr之前的字符串(如果它存在)。所以,对于上面的两个例子,结果应该是:runcmd-nameabcdxyz我在尝试:regexp.MustCompile(`(-name).+?=-descr`)但是,那没有返回任何匹配项。我想知道正确的正则表达式来获取字符串直到-descr如果它存在

regex - IPv4 正则表达式捕获地址的不正确部分

这个问题在这里已经有了答案:Matchexactstring(3个答案)关闭3年前。我正在尝试编写一个程序,从终端输入中打印IPv4地址的无效部分。这是我的代码:packagechapter4import("bufio""fmt""os""regexp""strings""time")funcIPV4(){varf*os.Filef=os.Stdindeferf.Close()scanner:=bufio.NewScanner(f)fmt.Println("Exercise1,Chapter4-DetectingincorrectpartsofIPv4Addresses,enteran

regex - 如何在我的 go 项目中修复此错误 - undefined : bson. RegEx

我的编辑器出现以下错误:未定义:bson.RegEx由于我的go项目中的这行代码:regex:=bson.M{"$regex":bson.RegEx{模式:id,选项:"i"}}为什么会出现此错误以及如何解决?我已确定我正在导入:"go.mongdb.org/mongo-driver/bson"我还检查了bson/primitive/primitive.go以查看RegEx确实存在使用版本1.1.0的mongo-driver。 最佳答案 设法通过删除这个来解决这个问题:regex:=bson.M{"$regex":bson.RegE

PHP/Go 套接字通信

我正在尝试使用套接字在Go和PHP之间进行通信。我使用的代码是:开始:fmt.Println("Launchingserver...")ln,_:=net.Listen("tcp",":8080")conn,_:=ln.Accept()for{message,_:=bufio.NewReader(conn).ReadString('\n')fmt.Print("MessageReceived:",string(message))conn.Write([]byte("test"+"\n"))}PHP:$address=gethostbyaddr($ip);$socket=socket_c

php - 高语 : create a function that accept an interface (I came from PHP)

在PHP中我可以创建一个接口(interface)interfaceHello{publicfunctionbar();}以及一些实现它的类finalclassFooimplementsHello{publicfunctionbar(){//dosomething}}finalclassBarimplementsHello{publicfunctionbar(){//dosomething}}然后,我还可以创建一个接受该接口(interface)的NewClass::bar()方法。finalclassNewClass{publicfunctionbar(Hello$hello){//

dictionary - 戈朗 : Using a defined interface in a Map Value

在链接的Playground示例中,我定义了一个类型:typeDoMapmap[int]func(Doer)stringDoer在我定义的接口(interface)类型中。我在具体类型MyDoer上实现接口(interface)。我希望能够构造一个DoMap,其中该映射中的条目包含如下两个函数:func(Doer)string//thisworksfunc(*MyDoer)string//thisdoesn'tfunc(MyDoer)string//more-or-lessthesameidea,alsodoesn't我不能,虽然很明显它们是不同的类型,但我想知道为什么我不能,因为函数

php - Golang中是否有相当于PHP的openssl_pkey_get_private?

我必须将PHP代码翻译成Golang,我遇到了这个问题。 最佳答案 Go当然可以加载x509私钥,但是没有openssl_pkey_get_private之类的“do-what-I-want”功能。PEM解码key(并可能解密它)后,使用x509package中的Parse*PrivateKey函数之一。:packagemainimport("crypto""crypto/x509""encoding/pem""fmt""io/ioutil""log""strings")funcmain(){pemBytes,err:=ioutil

regex - 从正则表达式构造字符串

我试图找到一种方法来从具有给定interface{}数组的已编译*regexp.Regexp构造string数据.例如:re:=regexp.MustCompile(`(?P\w+)\s*(?P\d+)`)我想通过string和int数据从re中找到的结构构造一个string可以作为接口(interface){}接收。不知道如何在Go中做到这一点。请帮帮我。提前致谢。 最佳答案 这样的库,常被称为Xeger,存在于许多语言中,包括go。然而,这个叫做regen:这是一个从Go/RE2正则表达式生成随机字符串的工具。这是一个例子:$r

go - PHP 在 golang 中的 fopen/fread/fwrite 等价物

在golang中有没有等价的Php的fopen/fread/fwrite方法?目前,我正在使用偏移量移动、写入并追加到[]byte,然后通过os.File.Write()写入所有内容。但我想知道是否有一种方法可以直接对文件进行操作。 最佳答案 fopenos.OpenFilehttps://golang.org/pkg/os/#OpenFilefread没有完全相同的匹配,但更相似(*os.File)Readhttps://golang.org/pkg/os/#File.Readfwrite(*os.File)写入https://g

go - 无法编译 Go 文件 - "initialization failure...xxxx redeclared in this block"

我是Go的新手,我按照website中的说明进行操作和youtubevideo当我运行gobuildhello.go时出现以下错误:go:disablingcache(/home/myuser/.cache/go-build)duetoinitializationfailure:open/home/myuser/.cache/go-build/log.txt:permissiondenied#runtime/usr/local/go/src/runtime/map.go:64:2:bucketCntBitsredeclaredinthisblockpreviousdeclaration